package com.bawei.module_mine.login

import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityLoginBinding
import com.bawei.module_mine.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = Router.LOGIN_PAGE)
class LoginActivity : BaseActivity<LoginViewModel,ActivityLoginBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    private val viewModel:LoginViewModel by viewModels()
    override fun initView() {
        super.initView()
        mBinding.toRegister.setOnClickListener {
            ARouter.getInstance().build(Router.REGISTER_PAGE).navigation()
        }
        mBinding.username.setText(AppMMKV.username)
        mBinding.password.setText(AppMMKV.password)
        mBinding.btnLogin.setOnClickListener {
            if (mBinding.username==null||mBinding.password==null){
                ToastUtils.showLong(this,"账号或密码不可为空")
            }
            viewModel.login(mBinding.username.text.toString(),mBinding.password.text.toString())
        }

    }

    override fun initListener() {
        viewModel.noticeState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"登录失败")
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"登录成功")
                    ARouter.getInstance().build(Router.MAIN_PAGE).navigation()
                }
            }
        }
    }

    override fun initData() {
        mBinding.viewmodel=viewModel

    }

    override fun initStatusBar() {

    }
}